capture log close continue
log using abs-rel-q4-continue.txt, name(continue) replace text

//  program:    abs-rel-q4-continue.do
//  task:		analysis of absolute and relative inequality judgments
// 	input:		exp1.dta						
//	output:		none
//  project:    MEDEC experiment 1
//  author:     sam harper \ 25mar2013

//  #0
//  program setup

version 12
set linesize 80
clear all
macro drop _all


// #1
// bring in experiment 1 data

use exp1.dta, clear
datasignature confirm

// #2
// Scenario: decreasing abs, increasing relative

* first test for EMM by scenario status
regress q4continue i.time##i.treatment##i.mag if scennum==2 | scennum==6, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.369, so drop product term 
* and adjust for scenario
regress q4continue i.time##i.treatment i.mag if scennum==2 | scennum==6, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q4s26
	
* predicted margins, by scenario
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Should the program continue?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q4s26, replace) ///
	ytitle("1 (def. not) ... 7 (very def. should)") ///
	note("Scenarios 2 and 6: Decreasing absolute, increasing relative inequality", size(small))

* marginal effects
margins, over(time) dydx(treatment) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]


// #3
// Scenario: constant absolute, increasing relative

* first test for EMM by scenario status
regress q4continue i.time##i.treatment##i.mag if scennum==7 | scennum==8, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.417, so drop product term 
* and adjust for scenario
regress q4continue i.time##i.treatment i.mag if scennum==7 | scennum==8, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q4s78

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Should the program continue?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q4s78, replace) ///
	ytitle("1 (def. not) ... 7 (very def. should)") ///
	note("Scenarios 7 and 8: Constant absolute, increasing relative inequality", size(small))

* marginal effects
margins, over(time) dydx(treatment) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]


// #4
// Scenario: decreasing abs, constant relative

* first test for EMM by scenario status
regress q4continue i.time##i.treatment##i.mag if scennum==3 | scennum==4, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.024, so keep product term 
estimates store q4s34

* predicted margins, by scenario
margins time#treatment#mag, cformat(%4.3f)
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q4s34, replace) ///
	by(mag) ytitle("1 (def. not) ... 7 (very def. should)", size(medsmall))  ///
	byopts(title("Should the program continue?") ///
	note("Scenarios 3 and 4: Decreasing absolute, constant relative inequality", size(small)))

* marginal effects
margins, dydx(treatment) over(mag time) cformat(%4.3f) post
lincom _b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]
lincom _b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time]
lincom (_b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]) ///
 	   - (_b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time])


// #5
// Scenario: decreasing abs, decreasing relative

* first test for EMM by scenario status
regress q4continue i.time##i.treatment##i.mag if scennum==1 | scennum==5, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.449, so drop product term 
* and adjust for scenario
regress q4continue i.time##i.treatment i.mag if scennum==1 | scennum==5, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q4s15

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Should the program continue?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q4s15, replace) ///
	ytitle("1 (def. not) ... 7 (very def. should)") ///
	note("Scenarios 1 and 5: Decreasing absolute, decreasing relative inequality", size(small))
	
* marginal effects
margins, over(time) dydx(treatment) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]


// #6
// Scenario: increasing abs, increasing relative

* first test for EMM by scenario status
regress q4continue i.time##i.treatment##i.mag if scennum==9 | scennum==10, ///
	cluster(subject) cformat(%4.3f) vsquish

* p-value for timeXtreatmentXmag = 0.915, so drop product term 
* and adjust for scenario
regress q4continue i.time##i.treatment i.mag if scennum==9 | scennum==10, ///
	cluster(subject) cformat(%4.3f) vsquish
estimates store q4s910

* predicted margins
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Should the program continue?", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q4s910, replace) ///
	ytitle("1 (def. not) ... 7 (very def. should)") ///
	note("Scenarios 9 and 10: Increasing absolute, increasing relative inequality", size(small))

* marginal effects
margins, over(time) dydx(treatment) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]


// #6
// table of all effect estimates
esttab q4s26 q4s78 q4s34 q4s15 q4s910 using q4est.rtf, ///
	b(%5.2f) ci(%5.2f) ///
	keep(1.time 1.treatment 1.time#1.treatment 1.mag 1.time#1.mag 1.treatment#1.mag 1.time#1.treatment#1.mag _cons) ///
	collabels(Coef. 95\%CI) nostar not replace compress ///
	title("Program continuation")

// #7
// put all results in a single graph, collapsing common scenarios
// and adjusting for scenario

* collapse scenarios
recode scennum (2 6 = 0 "Difference decreases, Ratio increases") ///
	(7 8 =1 "Difference constant, Ratio increases") ///
	(3 = 2 "Small diff. decrease, Ratio constant") ///
	(4 = 3 "Large diff. decrease, Ratio constant") ///
	(1 5 = 4 "Difference decreases, Ratio decreases") ///
	(9 10 = 5 "Difference increases, Ratio increases"), gen(grscen)
	
* predicted margins for inconsistent scenarios
qui regress q4continue i.grscen##(i.time##i.treatment) i.mag if grscen<4, ///
	cluster(subject) cformat(%4.3f) vsquish
qui margins time#treatment, over(grscen)
marginsplot, xsc(r(-.2 1.2)) xtitle("") by(grscen) ///
	byopts(rows(1) title("Scenarios with inconsistent absolute and relative inequality trends", size(medsmall)) ///
	legend(off) graphregion(lcolor(white) fcolor(white))) ///
	ylab(1(1)7) subtitle(, size(vsmall) fcolor(white) lcolor(white)) ///
	ytitle("Should the program continue?"  "1(def. not)...7(very def. should)", ///
	size(vsmall)) name(con, replace) fysize(42)

* predicted margins for consistent scenarios	
qui regress q4continue i.grscen##(i.time##i.treatment) i.mag if grscen>3 ///
	& grscen<11, cluster(subject) cformat(%4.3f) vsquish
qui margins time#treatment, over(grscen)
marginsplot, xsc(r(-.2 1.2)) xtitle(Time) by(grscen) ///
	byopts(rows(1) title("Scenarios with consistent absolute and relative inequality trends", size(medsmall)) ///
	graphregion(lcolor(white) fcolor(white) margin(l=20 r=20))) ///
	legend(order(3 "shown raw data and difference measure of inequality" 4 "shown raw data and ratio measure of inequality") ///
	cols(1) rowgap(0.5) size(small) symxsize(8) ///
	title("Experimental condition:", size(small)) width(95)) ///
	subtitle(, size(vsmall) fcolor(white) lcolor(white)) ///
	ytitle("Should the program continue?"  "1(def. not)...7(very def. should)", ///
	size(vsmall)) name(incon, replace) ylab(1(1)7)
	
graph combine con incon, rows(2) xsize(3.5) graphregion(lcolor(white) fcolor(white))

* predicted margins for exporting to R graphs	
qui regress q4continue i.grscen##(i.time##i.treatment) i.mag if grscen<11, ///
	cluster(subject)
margins time#treatment, over(grscen)

log close continue
exit

